package co.fitstart.weapp.api.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.persistence.annotation.Dao;

import co.fitstart.entity.course.CourseType;
import co.fitstart.entity.course.round.CourseRound;
import co.fitstart.entity.course.schedule.CourseSchedule;
import co.fitstart.entity.course.schedule.ScheduleStatus;
import co.fitstart.entity.user.Student;
import co.fitstart.entity.utils.DateFormatUtils;
import co.fitstart.entity.utils.DateUtils;
import co.fitstart.weapp.api.dao.CourseScheduleDao;
import co.fitstart.weapp.api.service.CourseScheduleService;

/**
 * 
 * @author dan
 *
 */
@Service("courseScheduleService")
public class CourseScheduleServiceImpl implements CourseScheduleService{
    
    @Dao
    private CourseScheduleDao courseScheduleDao;

    @Autowired
    private GenericDaoSupport genericDaoSupport;

    /*
     * (non-Javadoc)
     * @see co.fitstart.weapp.api.service.CourseRoundService#load(java.lang.Long)
     */
    public CourseSchedule load(Long courseScheduleId) {
        return genericDaoSupport.load(CourseSchedule.class, courseScheduleId);
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.weapp.api.service.CourseScheduleService#listFinishSchedule(co.fitstart.entity.course.round.CourseRound)
     */
    public List<CourseSchedule> list(CourseRound courseRound) {
        return courseScheduleDao.list(courseRound, ScheduleStatus.FINISH);
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.weapp.api.service.CourseScheduleService#listCouldSignin(co.fitstart.entity.user.Student)
     */
    public List<CourseSchedule> listCouldSignin(Student student) {
        List<CourseSchedule> couldSigninSchedules = new ArrayList<>();
        
        Date now = new Date();
        List<CourseSchedule> todaySchedules = courseScheduleDao.list(Arrays.asList(new CourseType[]{CourseType.INDIVIDUAL, CourseType.YEAR}), student, DateUtils.parseDate(DateFormatUtils.format(now, "yyyy-MM-dd"), "yyyy-MM-dd"), ScheduleStatus.READY);
        
        if(!todaySchedules.isEmpty()) {
            for(CourseSchedule courseSchedule : todaySchedules) {
                Date occurTime = DateUtils.parseDate(DateFormatUtils.format(courseSchedule.getOccurDate(), "yyyy-MM-dd") + " " + courseSchedule.getOccurHour() + ":00", "yyyy-MM-dd HH:mm:ss");
                if(occurTime.after(org.apache.commons.lang3.time.DateUtils.addHours(now, -2)) && org.apache.commons.lang3.time.DateUtils.addHours(now, 1).after(occurTime)) {
                    couldSigninSchedules.add(courseSchedule);
                }
            }
        }
        
        return couldSigninSchedules;
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.weapp.api.service.CourseScheduleService#isFirstReady(co.fitstart.entity.course.schedule.CourseSchedule)
     */
    public boolean isFirstReady(CourseSchedule courseSchedule) {
        List<CourseSchedule> readySchedules = courseScheduleDao.list(courseSchedule.getCourseRound(), Arrays.asList(new ScheduleStatus[]{ScheduleStatus.READY}), 0, 1);
        
        return !readySchedules.isEmpty() && readySchedules.get(0) == courseSchedule;
    }
}
